package com.mocha.bass.test.jiangan;

import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.vehicle.dmo.entity.CameraPGEntity;
import com.hyzh.latte.vehicle.repository.CameraPGRepository;
import com.mocha.bass.test.gaoxin.ExcelReadUtile;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/5/27 14:47
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class UpdateCamera {

    @Autowired
    private CameraPGRepository cameraPGRepository;

    @Test
    public void test(){

        List<ExcelInfo> excelInfoList = ExcelReadUtile.readeExcelData("d:/ja_dit.xlsx");

        List<CameraPGEntity> list = cameraPGRepository.lambdaQuery().eq(CameraPGEntity::getParkCode, "jiangan").list();
        Map<String, CameraPGEntity> map = list.stream().collect(Collectors.toMap(CameraPGEntity::getCameraCode, Function.identity()));


        for (ExcelInfo info : excelInfoList){
            CameraPGEntity oldEntity = getCamera(info.getColumn1().trim(), map);
            if (null == oldEntity){
                System.out.println("Old is null "+ info.toString());
            }else {
                CameraPGEntity n = new CameraPGEntity();
                n.setId(oldEntity.getId());
                n.setDirection(Integer.parseInt(info.getColumn2().trim()));
                cameraPGRepository.updateById(n);
            }
        }



    }


    private CameraPGEntity getCamera(String oldCno, Map<String, CameraPGEntity> map){
      /*  String cno = oldCno;
        if (oldCno.startsWith("510")){
            cno = oldCno.replace("510", "");
        }else if (oldCno.startsWith("51")){
            cno = oldCno.replace("51", "");
        }*/

        return map.get(oldCno);
    }
}
